package com.pi.pro.xcmg.am.was.web.cust.controller;

import com.alibaba.fastjson.JSONArray;
import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.DateUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.framework.core.util.csv.CsvExporter;
import com.pi.framework.web.util.CsvView;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.VwtActShipTransList;
import com.pi.wms.service.ShipTransService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
@RequestMapping("/outbound/shipfulltrans")
public class ShipFullTransController extends BaseCrudController<VwtActShipTransList, Long> {

	@Autowired
	private ShipTransService service;
	
	
	@Override
	protected ICrudService<VwtActShipTransList, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "shipfulltrans";
	}

	@Override
	protected VwtActShipTransList newModel(HttpServletRequest request) {
		return new VwtActShipTransList();
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		query.put("type", WmsConstants.TRANS_TYPE_SHIP);
        query.put(WmsConstants.ACT_QUERY_OPTION, WmsConstants.ACT_QUERY_OPTION_LIST);
		super.prepareDatatableConditions(query, input);
	}
	
	@PostMapping(value = { "/export" })
	public ModelAndView export(@RequestParam Map<String, Object> query, DataTablesInput input) {
		query.put("type", WmsConstants.TRANS_TYPE_SHIP);
		String inputParams = "";
		if (query.get("input")!=null){
			inputParams=query.get("input").toString();
		}
		if (StringUtils.isNotEmpty(inputParams)) {
			input = JSONArray.parseObject(inputParams, DataTablesInput.class);
		}
		input.setLength(-1);
		String ymd = DateUtils.format(new Date(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HHMMSS);
		final DataTablesInput ins = input;
        CsvExporter<VwtActShipTransList> exporter = new CsvExporter<VwtActShipTransList>() {
            @Override
            public String[] header() {
                return "交易号,物料,物料名称,出库单号,订单数量,发货数量,外部单号,订单号,订单WBS,工位,托盘码,源库位,交易时间,操作员"
                        .split(",");
            }

            @Override
            public Collection<VwtActShipTransList> nextList(int pageSize, int page) {
                ins.setLength(pageSize);
                ins.setStart(page * pageSize);
                DataTablesOutput<VwtActShipTransList> out = service.findAll(ins, query);
                return out == null ? null : out.getData();
            }

            @Override
            public Collection<String> toText(VwtActShipTransList d) {
                List<String> item = new ArrayList<String>();
                item.add(d.getNumber() != null ? d.getNumber() : "");
                item.add(d.getMaterialCode() != null ? d.getMaterialCode() : "");
                item.add(d.getMaterialName() != null ? d.getMaterialName() : "");
                item.add(d.getDocNumber() != null ? d.getDocNumber() : "");
                item.add(d.getQtyOrderedEach() != null ? String.valueOf(d.getQtyOrderedEach()) : "");
                item.add(d.getQtyTransEach() != null ? String.valueOf(d.getQtyTransEach()) : "");
                item.add(d.getSoRefDocNumber() != null ? d.getSoRefDocNumber() : "");
                item.add(d.getRefAsnNumber() != null ? d.getRefAsnNumber() : "");
                item.add(d.getPaymentTerms() != null ? d.getPaymentTerms() : "");
                item.add(d.getPlanLoadDoor() != null ? d.getPlanLoadDoor() : "");
                item.add(d.getFromContainNumber() != null ? d.getFromContainNumber() : "");
                item.add(d.getFromLocCode() != null ? d.getFromLocCode() : "");
                item.add(DateUtils.format(d.getOpenTime(), DateUtils.FORMAT_DATE_YYYY_MM_DD_HH_MM_SS));
                item.add(d.getCreatedBy() != null ? d.getCreatedBy() : "");
                return item;
            }

        };
        return new ModelAndView(new CsvView(exporter, "shipfulltrans_" + ymd));
	}
}
